<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-select v-model="filters.appName.value" placeholder="应用名" @change="createEventTypeNameList">
						<el-option
						v-for="item in filters.appName.options"
						:key="item.value"
						:label="item.label"
						:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="filters.eventTypeName.value" placeholder="分类">
						<el-option
						v-for="item in filters.eventTypeName.options"
						:key="item.value"
						:label="item.label"
						:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<template>
						<div style="width:200px;display:inline-block;padding-right:10px;padding-left:10px;">
							<el-input v-model="inputName" width="200" placeholder="请输入名称"></el-input>
						</div>
					</template>
				<el-form-item>
					<el-button type="primary" v-on:click="getEventIdList">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="eventIdList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" label="序号" width="70">
			</el-table-column>
			<el-table-column prop="appName" label="应用名" min-width="200" sortable>
			</el-table-column>
			<el-table-column prop="eventTypeName" label="分类" min-width="120" sortable>
			</el-table-column>
			<el-table-column prop="eventId" label="事件id" min-width="300" sortable>
			</el-table-column>
			<el-table-column prop="describe" label="描述" min-width="300" sortable>
			</el-table-column>
			<el-table-column prop="remarks" label="备注" min-width="300" sortable>
			</el-table-column>
			<el-table-column prop="updateTime" label="更新时间" min-width="170" sortable>
			</el-table-column>
			<el-table-column label="操作" min-width="150" fixed="right">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="应用名" prop="appName">
					<el-input v-model="editForm.appName" auto-complete="off" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="分类" prop="eventTypeName">
					<el-input v-model="editForm.eventTypeName" auto-complete="off" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="事件id" prop="eventId">
					<el-input v-model="editForm.eventId" :disabled="true" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="describe">
					<el-input type="textarea" v-model="editForm.describe" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="备注" prop="remarks">
					<el-input type="textarea" v-model="editForm.remarks" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增eventId" v-model="addFormVisible" :close-on-click-modal="false" size="large">
			<el-form :model="addForm" label-width="0px" :rules="addFormRules" ref="addForm">
				<el-form-item>
					<el-select v-model="filters.appName.value" placeholder="应用名">
						<el-option
						v-for="item in filters.appName.options"
						:key="item.value"
						:label="item.label"
						:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<el-table :data="addForm.list" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
				<el-table-column type="index" label="序号" width="65">
				</el-table-column>
				<el-table-column prop="eventTypeName" label="分类">
					<template scope="scope">
						<el-input v-model="scope.row.eventTypeName" auto-complete="off"></el-input>
					</template>
				</el-table-column>
				<el-table-column prop="eventId" label="事件id">
					<template scope="scope">
						<el-input v-model="scope.row.eventId" auto-complete="off"></el-input>
					</template>
				</el-table-column>
				<el-table-column prop="describe" label="描述">
					<template scope="scope">
						<el-input v-model="scope.row.describe" auto-complete="off"></el-input>
					</template>
				</el-table-column>
				<el-table-column prop="remarks" label="备注">
					<template scope="scope">
						<el-input v-model="scope.row.remarks" auto-complete="off"></el-input>
					</template>
				</el-table-column>
			</el-table>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser , getAppIdAndEventType , getAppListEvent , addEventList , deleteEvent , updateEvent } from '../../api/api';
	export default {
		data() {
			return {
				filters: {
					appName:{
						options: [
						],
						value: ''
					},
					eventTypeName:{
						options: [
							
						],
						value: ''
					},
					data:{
						
					}
				},
				eventIdList: [],
				total: 0,
				page: 1,
				pageSize:50,
				listLoading: false,
				sels: [],//列表选中列
				inputName:'',
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					appName:'',
					eventTypeName:'',
					eventId:'',
					describe:'',
					remarks:'',
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					list:[
						{
							eventTypeName:'',
							eventId:'',
							describe:'',
							remarks:'',
						}	
					]
				}

			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getEventIdList();
			},
			//获取应用列表和二级分类列表
			getAppIdAndEventType(){
				getAppIdAndEventType().then((res)=>{
					console.log("getAppIdAndEventType",res);
					this.filters.data = res.data.data;
					this.filters.appName = this.createAppNameList();
				})
			},
			createAppNameList(){
				let options=[];
				for(let key in this.filters.data){
					options.push({
						value:key,
						label:this.filters.data[key].appName,
					})
				}
				return {
					options,
					value:"",
				}
			},
			createEventTypeNameList(appId){
				console.log("appId",appId)
				let options = [];
				let list = this.filters.data[appId].eventTypeName;
				options=list.map((item)=>{
					return {
						value:item,
						label:item,
					}
				})
				this.filters.eventTypeName={
					options,
					value:"",
				}
			},
			//获取用户列表
			getEventIdList() {
				let para = {
					page: this.page,
					pageSize:this.pageSize,
					keyword: this.filters.keyword,
					appId:this.filters.appName.value,
					eventTypeName:this.filters.eventTypeName.value,
					inputName:this.inputName
				};
				this.listLoading = true;
				//NProgress.start();
				getAppListEvent(para).then((res) => {
					console.log(res.data.data)
					this.total = res.data.data.total;
					this.eventIdList = res.data.data.eventList.map(item=>{
						util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd')
						return Object.assign({},item,{
							insertTime:util.formatDate.format(new Date(item.insertTime), 'yyyy-MM-dd hh:mm:ss'),
							updateTime:util.formatDate.format(new Date(item.updateTime), 'yyyy-MM-dd hh:mm:ss'),
						})
					});
					this.listLoading = false;
					//NProgress.done();
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { eventIds: row.eventId };
					deleteEvent(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEventIdList();
						this.getAppIdAndEventType();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				// this.addForm = {
				// 	name: '',
				// 	sex: -1,
				// 	age: 0,
				// 	birth: '',
				// 	addr: ''
				// };
				this.initData();
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							updateEvent(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getEventIdList();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					let appId = this.filters.appName.value;
					if(appId){
						if (valid) {
							this.$confirm('确认提交吗？', '提示', {}).then(() => {
								this.addLoading = true;
								//NProgress.start();
								let list=[];
								let appName = this.filters.data[appId].appName;
								this.addForm.list.map(item=>{
									if(item.eventTypeName && item.eventId){
										list.push(Object.assign({}, item , {appId,appName}))
									}
								})
								let para = Object.assign({}, {app:JSON.stringify(list)});
								// para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
								addEventList(para).then((res) => {
									if(res.data.code=="1000"){

										let fail = res.data.data.fail
										this.addLoading = false;
										let arr=[];
										this.getEventIdList();
										if(fail && fail.length>0){
											this.$message({
												message: '提交失败',
												type: 'error'
											});
											this.addForm.list.map((item,i)=>{
												if(fail[i]){
													arr.push(list[fail[i]-1]);
												}else{
													arr.push({
														eventTypeName:'',
														eventId:'',
														describe:'',
														remarks:'',
													})
												}
											})
											console.log(arr);
											this.addForm.list = arr;
										}else{
											//NProgress.done();
											this.$message({
												message: '提交成功',
												type: 'success'
											});
											this.$refs['addForm'].resetFields();
											this.addFormVisible = false;
										}
									}
								});
							});
						}
					}else{
						this.$message({
							message: '请选择应用',
							type: 'error'
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEventIdList();
					});
				}).catch(() => {

				});
			},
			//初始化data
			initData:function(){
				//新增界面数据初始化
				let list=[];
				for(let i=0;i<10;i++){
					let item={
						eventTypeName:'',
						eventId:'',
						describe:'',
						remarks:'',
					}
					list.push(item);
				}
				this.addForm = {
					list:list,
				}
				console.log("list",list)
			}
		},
		mounted() {
			this.initData();
			this.getEventIdList();
			this.getAppIdAndEventType()
		}
	}

</script>

<style scoped>

</style>